var database = require('../../data/data.js');
Page({
  data: {
    search_data: "",
    pageNum: 1,
    history: false,
    onshow: false,
  },
  onLoad: function (options) {
    var that = this;
    var date = new Date();
    var month = "";
    if (date.getMonth() < 9) {
      month = "0" + (parseInt(date.getMonth()) + 1)
    } else {
      month = parseInt(date.getMonth()) + 1
    }
    this.setData({
      year: date.getFullYear(),
      month: month
    })
    if (options.date) {
      this.setData({
        date: options.date,
        history: true
      })
    } else {
      this.setData({
        date: that.data.year + that.data.month
      })
    }
    var keys = wx.getStorageInfoSync().keys;
    if (keys != "") {
      var historys = [];
      for (var i = 0; i < keys.length; i++) {
        var history = {
          value: "",
          length: ""
        }
        var value = wx.getStorageSync(keys[i])
        history.value = value
        history.length = value.length
        historys[i] = history
      }
      this.setData({
        historys: historys
      })
    }
  },
  onShow() {
    if (database.vote.length > 0) {
      var integralList = this.data.integralList
      for (var i = 0; i < this.data.integralList.length; i++) {
        for (var j = 0; j < database.vote.length; j++) {
          if (database.vote[j].id == integralList[i].id) {
            integralList[i].integralTotal = parseInt(integralList[i].integralTotal) + 1;
            integralList[i].vote = true;
          }
        }
      }
      database.vote = [];
      this.setData({
        integralList: integralList
      })
    }
  },
  save_data: function (e) {
    this.setData({
      search_data: e.detail.value
    })
  },
  search: function () {
    if (this.data.search_data != "") {
      wx.showLoading({
        title: '资源加载中',
        mask: true
      })
      var that = this;
      var flag = wx.getStorageSync("history" + 1);
      if (flag == "") {
        wx.setStorageSync("history" + 1, this.data.search_data)
      } else {
        var flag1 = true;
        var historys_keys = wx.getStorageInfoSync().keys
        for (var i = 0; i < historys_keys.length; i++) {
          if (wx.getStorageSync(historys_keys[i]) == this.data.search_data) {
            flag1 = false;
          }
        }
        if (flag1) {
          if (historys_keys.length == 10) {
            for (var i = historys_keys.length; i > 1; i--) {
              wx.setStorageSync("history" + i, wx.getStorageSync(historys_keys[i - 2]))
            }
          } else {
            for (var i = historys_keys.length; i > 0; i--) {
              wx.setStorageSync("history" + (i + 1), wx.getStorageSync(historys_keys[i - 1]))
            }
          }
          wx.setStorageSync("history1", this.data.search_data)
        }
      }
      this.setData({
        pageNum: 1,
        softballName: that.data.search_data
      })
      var pageNum = this.data.pageNum;
      wx.request({
        url: database.url + '/api/V2/integral/integral_list',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          accessToken: database.accessToken,
          regionName: "other",
          pageNo: pageNum,
          pageSize: 10,
          softballName: that.data.search_data,
          yearMonth: that.data.date,
          unionid: database.unionid
        },
        success(res) {
          console.log(res)
          ++pageNum;
          that.setData({
            pageNum: pageNum
          })
          var integralList = res.data.data.integralList;
          that.setData({
            integralList: integralList,
            result: integralList.length
          })
          wx.hideLoading();
        }
      })
    } else {
      wx.showToast({
        icon: "none",
        title: '关键字不能为空',
      })
    }
  },
  vote: function (e) {
    var vote = {
      id: e.currentTarget.dataset.id
    }
    database.vote[database.vote.length] = vote;
    wx.showLoading({
      title: '正在保存',
    })
    var id = e.currentTarget.dataset.id;
    var teamList = this.data.integralList;
    for (var i = 0; i < teamList.length; i++) {
      if (teamList[i].id == id) {
        teamList[i].integralTotal = parseInt(teamList[i].integralTotal) + 1;
      }
    }
    this.setData({
      integralList: teamList
    })
    wx.request({
      url: database.url + '/api/V2/integral/integral_vote',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken,
        regionName: "other",
        softballId: id,
        weChatId: "DB80F6CF1D0D4D1AB8F7B5C0A04CA704",
        unionid: database.unionid,
        integralGenre: "TouPiao"
      },
      success(res) {
        wx.hideLoading();
        wx.showToast({
          icon:"none",
          title: res.data.status_reason,
        })
      }
    })
  },
  history: function (e) {
    wx.showLoading({
      title: '资源加载中',
      mask: true
    })
    var that = this;
    this.setData({
      pageNum: 1,
      softballName: e.currentTarget.dataset.history
    })
    var pageNum = this.data.pageNum;
    wx.request({
      url: database.url + '/api/V2/integral/integral_list',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken,
        regionName: "other",
        pageNo: pageNum,
        pageSize: 10,
        yearMonth: that.data.date,
        softballName: that.data.softballName,
        unionid: database.unionid
      },
      success(res) {
        ++pageNum;
        that.setData({
          pageNum: pageNum
        })
        var integralList = res.data.data.integralList;
        that.setData({
          integralList: integralList,
          result: integralList.length
        })
        wx.hideLoading();
      }
    })
  },
  onReachBottom: function () {
    var that = this;
    var integralList1 = [];
    var pageNum = that.data.pageNum;
    if (this.data.result >= 10) {
      wx.showLoading({
        title: '资源加载中',
        mask: true
      })
      wx.request({
        url: database.url + '/api/V2/integral/integral_list',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          accessToken: database.accessToken,
          regionName: "other",
          pageNo: pageNum,
          pageSize: 10,
          yearMonth: that.data.date,
          softballName: that.data.softballName,
          unionid: database.unionid
        },
        success(res) {
          ++pageNum;
          var integralList2 = res.data.data.integralList;
          for (var i = 0; i < that.data.integralList.length + integralList2.length; i++) {
            if (i < that.data.integralList.length) {
              integralList1[i] = that.data.integralList[i];
            } else {
              integralList1[i] = integralList2[i - that.data.integralList.length];
            }
          }
          that.setData({
            integralList: integralList1,
            pageNum: pageNum
          })
          if (integralList2.length < 10) {
            that.setData({
              noData: true
            })
          }
          wx.hideLoading();
        }
      })
    }
  },
  toDetail: function (e) {
    this.setData({
      onshow: true
    })
    if (this.data.history) {
      wx.navigateTo({
        url: '/pages/team_detail/team_detail?id=' + e.currentTarget.dataset.id + '&type=history',
      })
    } else {
      wx.navigateTo({
        url: '/pages/team_detail/team_detail?id=' + e.currentTarget.dataset.id + '&type=vote',
      })
    }
  },
})